package cn.collin.commons.codec;

/**
 * 
 * This class contains a utility method for encoding a javascript string so that
 * it may be executing in browser.
 * <p>
 * Reference: <a
 * href="http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Literals#String_Literals">
 * Core JavaScript 1.5 Guide </a>
 * 
 * origal source code from org.springframework.web.util.JavaScriptUtils
 * 
 * NEEDTO REFINE
 * 
 * @author collin.code@gmail.com
 * 
 */
public class JavaScriptEncoder {
	/**
	 * Turn special characters into escaped characters conforming to JavaScript.
	 * Handles complete character set defined in HTML 4.01 recommendation.
	 * 
	 * @param input
	 *            the input string
	 * @return the escaped string
	 */
	public String encode(String input) {
		if (input == null) {
			return input;
		}

		StringBuffer filtered = new StringBuffer(input.length());
		char prevChar = '\u0000';
		char c;
		for (int i = 0; i < input.length(); i++) {
			c = input.charAt(i);
			if (c == '"') {
				filtered.append("\\\"");
			} else if (c == '\'') {
				filtered.append("\\'");
			} else if (c == '\\') {
				filtered.append("\\\\");
			} else if (c == '/') {
				filtered.append("\\/");
			} else if (c == '\t') {
				filtered.append("\\t");
			} else if (c == '\n') {
				if (prevChar != '\r') {
					filtered.append("\\n");
				}
			} else if (c == '\r') {
				filtered.append("\\n");
			} else if (c == '\f') {
				filtered.append("\\f");
			} else {
				filtered.append(c);
			}
			prevChar = c;

		}
		return filtered.toString();
	}
}
